/**
* 
*/
package com.ydy.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ydy.annotation.CtrlParam;
import com.ydy.annotation.StaffToken;
import com.ydy.annotation.UserToken;
import com.ydy.controller.base.BaseController;
import com.ydy.exception.BusinessException;
import com.ydy.ienum.EnumSystem;
import com.ydy.remote.wechat.WeChatApi;
import com.ydy.remote.wechat.vo.JsTicket;
import com.ydy.remote.wechat.vo.WechatResult;
import com.ydy.remote.wechat.vo.menu.Menu;
import com.ydy.service.user.UserService;
import com.ydy.service.wechat.WechatService;
import com.ydy.utils.IpUtil;
import com.ydy.utils.XmlUtil;
import com.ydy.vo.other.BaseVo;
import com.ydy.vo.token.UserTokenVo;

/**
 * @author xuzhaojie
 *
 *         2019年1月18日 上午9:17:29
 */
@Controller
@RequestMapping(value = "wechat", name = "微信模块")
public class WechatController extends BaseController {
	private final static Logger log = LoggerFactory.getLogger(WechatController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private WechatService wechatService;

	@RequestMapping(value = "wechatLogin", name = "微信登录跳转地址")
	public String wechatLogin(String url) {
		String redirecturl = WeChatApi.getLoginUrl(url);
		return "redirect:" + redirecturl;
	}

	@PostMapping(value = "login", name = "微信登录")
	@ResponseBody
	public ResponseEntity<UserTokenVo> login(@CtrlParam("code") String code) {
		return ResponseEntity.status(HttpStatus.OK).body(userService.loginByWechat(code));
	}

	@GetMapping(value = "jsticket", name = "微信Jsticket")
	@ResponseBody
	public ResponseEntity<JsTicket> jsticket() {
		return ResponseEntity.status(HttpStatus.OK).body(WeChatApi.getJsTicket());
	}

	@UserToken
	@GetMapping(value = "pay", name = "微信支付订单")
	@ResponseBody
	public ResponseEntity<Object> pay(@CtrlParam("订单ID") Long orderId, HttpServletRequest request) {
		String remoteIp = IpUtil.getIp(request);
		return ResponseEntity.status(HttpStatus.OK).body(wechatService.unifiedOrder(orderId, getUser(), remoteIp));
	}

	@UserToken
	@GetMapping(value = "refund", name = "微信退款订单")
	@ResponseBody
	public ResponseEntity<Object> refund(@CtrlParam("订单ID") Long orderId, String remark) {
		return ResponseEntity.status(HttpStatus.OK).body(wechatService.refund(orderId, remark, getUser(), null));
	}

	@StaffToken
	@GetMapping(value = "shopRefund", name = "店铺主动退款")
	@ResponseBody
	public ResponseEntity<Object> shopRefund(@CtrlParam("订单ID") Long orderId, String remark) {
		return ResponseEntity.status(HttpStatus.OK).body(wechatService.refund(orderId, remark, null, getStaff()));
	}

	@PostMapping(value = "notify", name = "微信支付回调")
	@ResponseBody
	public ResponseEntity<Object> notify(HttpServletRequest request) {
		String xml = getBodyString(request);
		return ResponseEntity.status(HttpStatus.OK).body(wechatService.notify(xml));
	}

	@PostMapping(value = "notifyRefund", name = "微信退款回调")
	@ResponseBody
	public ResponseEntity<Object> notifyRefund(HttpServletRequest request) {
		String xml = getBodyString(request);
		return ResponseEntity.status(HttpStatus.OK).body(wechatService.notifyRefund(xml));
	}

	@RequestMapping(value = "message", method = { RequestMethod.GET, RequestMethod.POST }, name = "微信信息处理")
	@ResponseBody
	public ResponseEntity<Object> message(WechatResult result, HttpServletRequest request) {
		if (StringUtils.isEmpty(result.getTimestamp()) || StringUtils.isEmpty(result.getNonce())) {
			throw new BusinessException(EnumSystem.PARAM_ERROR);
		}
		String sign = WeChatApi.makeSign(result.getTimestamp(), result.getNonce());
		if (RequestMethod.GET.toString().equalsIgnoreCase(request.getMethod())) {
			log.info("wechat传入signature={},系统加密sign={}", result.getSignature(), sign);
			return ResponseEntity.status(HttpStatus.OK).body(result.getEchostr());
		} else if (RequestMethod.POST.toString().equalsIgnoreCase(request.getMethod())) {
			String xml = getBodyString(request);
			return ResponseEntity.status(HttpStatus.OK).body(XmlUtil.objectToXml(wechatService.handleMessage(xml)));
		}
		return ResponseEntity.status(HttpStatus.OK).body(result.getEchostr());
	}

	@PostMapping(value = "createMenu", name = "微信菜单创建")
	@ResponseBody
	public ResponseEntity<BaseVo> createMenu(@RequestBody Menu menu) {
		return ResponseEntity.status(HttpStatus.OK).body(wechatService.createMenu(menu));
	}
}